/**
* @description: 周赛309 t3
* @author hewei
* @date 2022/9/4 11:03
* @version 1.0
*/

public class LongestNiceSubarray {

    public int longestNiceSubarray1(int[] nums) {
        int[] count = new int[32];
        int l = 0;
        int r = 0;
        int ans = 0;
        while (r < nums.length) {
            int temp = nums[r];
            for (int i = 0; i < 32; i++) {
                if ((temp & 1) == 1) count[i] += 1;
                temp >>= 1;
            }
            boolean flag = true;
            for (int i = 0; i < 32; i++) {
                if (count[i] > 1) {
                    flag = false;
                    break;
                }
            }
            if (!flag) {
                temp = nums[l];
                for (int i = 0; i < 32; i++) {
                    if ((temp & 1) == 1) count[i] -= 1;
                    temp >>= 1;
                }
                ++l;
            } else ans = Math.max(ans, r - l + 1);
            ++r;
        }
        return ans;
    }

    public int longestNiceSubarray(int[] nums) {
        int temp = 0;
        int l = 0;
        int r = 0;
        int ans = 0;
        while (r < nums.length) {
            while ((temp & nums[r]) != 0 && l < r) {
                temp ^= nums[l++];
            }
            temp |= nums[r++];
            ans = Math.max(ans, r - l);
        }
        return ans;
    }
}
